import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'dart:async';
import 'dart:async';
import 'dart:async';
import '../model/post.dart';

class DataTableDemo extends StatefulWidget {
  @override
  // _DataTableDemoState createState() => _DataTableDemoState();
  _DataTableDemoState2 createState() => _DataTableDemoState2();
}

enum Action { OK, CANCEL }

class _DataTableDemoState extends State<DataTableDemo> {
  int _sortColumnIndex = 0;
  bool _sortAscending = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DataTableDemo'),
        elevation: 0,
      ),
      body: Column(
        children: [
          Container(
            height: 300,
            padding: EdgeInsets.all(1),
            child: ListView(
              children: [
                DataTable(
                  sortColumnIndex: _sortColumnIndex,
                  sortAscending: _sortAscending,
                  onSelectAll: (value) {},
                  columns: [
                    DataColumn(
                      label: Text('A'),
                      onSort: (int index, bool ascending) {
                        setState(() {
                          _sortColumnIndex = index;
                          _sortAscending = ascending;
                          posts.sort((a, b) {
                            if (!ascending) {
                              final c = a;
                              a = b;
                              b = c;
                            }
                            return a.title.length.compareTo(b.title.length);
                          });
                        });
                      },
                    ),
                    DataColumn(
                      label: Text('T'),
                    ),
                    DataColumn(
                      label: Container(
                        width: 100,
                        child: Text('a'),
                      ),
                    ),
                  ],
                  rows: posts.map((post) {
                    return DataRow(
                      selected: post.selected,
                      onSelectChanged: (value) {
                        setState(() {
                          if (post.selected != value) {
                            post.selected = value!;
                          }
                        });
                      },
                      cells: [
                        DataCell(Text(post.title)),
                        DataCell(Text(post.author)),
                        DataCell(
                          Container(
                            padding: EdgeInsets.all(2),
                            child: Image.network(
                              post.imageUrl,
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),
                      ],
                    );
                  }).toList(),
                  /* rows: [
                    DataRow(
                      cells: [
                        DataCell(Text('h')),
                        DataCell(Text('w')),
                      ],
                    ),
                    DataRow(
                      cells: [
                        DataCell(Text('h1')),
                        DataCell(Text('w1')),
                      ],
                    ),
                    DataRow(
                      cells: [
                        DataCell(Text('h3')),
                        DataCell(Text('w3')),
                      ],
                    ),
                  ],*/
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class _DataTableDemoState2 extends State<DataTableDemo> {
  int _sortColumnIndex = 0;
  bool _sortAscending = true;
  final PostsDataSource _postsDataSource = PostsDataSource();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DataTableDemo'),
        elevation: 0,
      ),
      body: Container(
        padding: EdgeInsets.all(1),
        child: ListView(
          children: [
            PaginatedDataTable(
              source: _postsDataSource,
              header: Text('posts'),
              sortColumnIndex: _sortColumnIndex,
              sortAscending: _sortAscending,
              rowsPerPage: 5,
              columns: [
                DataColumn(
                  label: Text('A'),
                  onSort: (int index, bool ascending) {
                    _postsDataSource._sort(
                        (post) => post.title.length, ascending);
                    setState(() {
                      _sortColumnIndex = index;
                      _sortAscending = ascending;
                    });
                  },
                ),
                DataColumn(
                  label: Text('T'),
                ),
                DataColumn(
                  label: Container(
                    width: 100,
                    child: Text('a'),
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

class PostsDataSource extends DataTableSource {
  final List<Post> _posts = posts2;
  int _selectedCount = 0;

  void _sort(getField(post), bool ascending) {
    _posts.sort(
      (a, b) {
        if (!ascending) {
          final c = a;
          a = b;
          b = c;
        }
        final aValue = getField(a);
        final bValue = getField(b);
        return Comparable.compare(aValue, bValue);

      },
    );
    notifyListeners();
  }

  @override
  DataRow? getRow(int index) {
    Post post = _posts[index];
    return DataRow.byIndex(
      cells: [
        DataCell(Text(post.title)),
        DataCell(Text(post.author)),
        DataCell(
          Container(
            padding: EdgeInsets.all(2),
            child: Image.network(
              post.imageUrl,
              fit: BoxFit.cover,
            ),
          ),
        ),
      ],
    );
  }

  @override
// TODO: implement isRowCountApproximate
  bool get isRowCountApproximate => false;

  @override
// TODO: implement rowCount
  int get rowCount => _posts.length;

  @override
// TODO: implement selectedRowCount
  int get selectedRowCount => _selectedCount;
}
